
public class PlayerSkeleton 
{
	
	private static final int speed = 100; // the lower the faster (ms between pieces)

	// Returns the new height if putting the current piece in the specified 
	// slot with the specified orientation
	private int newHeight(State s, int[][] legalMoves, int orient, int slot)
	{
		int nextPiece = s.getNextPiece(); // the piece in play
		int top[] = s.getTop();
		
		// Properties of the pieces in the tetris game
		int pBottom[][][] = State.getpBottom();
		int pWidth[][] = State.getpWidth();
		
		// The maximum height
		int height = 0;
		
		// Calculate the maximum height for each column that is affected
		// by the current piece
		for (int c = 0; c < pWidth[nextPiece][orient]; c++)
			height = Math.max(height, top[slot + c] - pBottom[nextPiece][orient][c]);
		return height;
	}
	
	// Returns which move to pick based on the current playing state
	// and the array of available moves.
	public int pickMove(State s, int[][] legalMoves) 
	{	
		// Minimize the height by checking which move results in the minimum height
		int minHeight = State.ROWS + 1; // Infinity
		int bestMove = -1;
		for (int i = 0; i < legalMoves.length; i++)
		{
			int curHeight = newHeight(s, legalMoves, legalMoves[i][0], legalMoves[i][1]);
			if (curHeight < minHeight)
			{
				minHeight = curHeight;
				bestMove = i;
			}
		}
		return bestMove;
	}
	
	public static void main(String[] args) {
		State s = new State();
		new TFrame(s);
		PlayerSkeleton p = new PlayerSkeleton();
		while(!s.hasLost()) {
			s.makeMove(p.pickMove(s,s.legalMoves()));
			s.draw();
			s.drawNext(0,0);
			try {
				Thread.sleep(speed);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("You have completed "+s.getRowsCleared()+" rows.");
	}
}
